From 1e01f489c219388184f4703444e56815894f5773 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Mon, 2 May 2016 20:59:39 -0400 Subject: [PATCH] Use constructor pattern for `project_layout` in `toml.rs`. --- src/cargo/ops/cargo_read_manifest.rs | 4 +- src/cargo/util/toml.rs | 65 ++++++++++++++-------------- 2 files changed, 34 insertions(+), 35 deletions(-) diff --git a/src/cargo/ops/cargo_read_manifest.rs b/src/cargo/ops/cargo_read_manifest.rs index 445a356c4..56ae6a5e3 100644 --- a/src/cargo/ops/cargo_read_manifest.rs +++ b/src/cargo/ops/cargo_read_manifest.rs @@ -6,7 +6,7 @@ use std::path::{Path, PathBuf}; use core::{Package, Manifest, SourceId, PackageId}; use util::{self, paths, CargoResult, human, Config, ChainError}; use util::important_paths::find_project_manifest_exact; -use util::toml::{Layout, project_layout}; +use util::toml::Layout; pub fn read_manifest(contents: &[u8], layout: Layout, source_id: &SourceId, config: &Config) @@ -23,7 +23,7 @@ pub fn read_package(path: &Path, source_id: &SourceId, config: &Config) trace!("read_package; path={}; source-id={}", path.display(), source_id); let data = try!(paths::read(path)); - let layout = project_layout(path.parent().unwrap()); + let layout = Layout::from_project_path(path.parent().unwrap()); let (manifest, nested) = try!(read_manifest(data.as_bytes(), layout, source_id, config)); diff --git a/src/cargo/util/toml.rs b/src/cargo/util/toml.rs index 191e48b34..d0b2fe17f 100644 --- a/src/cargo/util/toml.rs +++ b/src/cargo/util/toml.rs @@ -32,6 +32,38 @@ pub struct Layout { } impl Layout { + /// Returns a new `Layout` for a given root path. + /// The `root_path` represents the directory that contains the `Cargo.toml` file. + pub fn from_project_path(root_path: &Path) -> Layout { + let mut lib = None; + let mut bins = vec![]; + let mut examples = vec![]; + let mut tests = vec![]; + let mut benches = vec![]; + + let lib_canidate = root_path.join("src").join("lib.rs"); + if fs::metadata(&lib_canidate).is_ok() { + lib = Some(lib_canidate); + } + + try_add_file(&mut bins, root_path.join("src").join("main.rs")); + try_add_files(&mut bins, root_path.join("src").join("bin")); + + try_add_files(&mut examples, root_path.join("examples")); + + try_add_files(&mut tests, root_path.join("tests")); + try_add_files(&mut benches, root_path.join("benches")); + + Layout { + root: root_path.to_path_buf(), + lib: lib, + bins: bins, + examples: examples, + tests: tests, + benches: benches, + } + } + fn main(&self) -> Option<&PathBuf> { self.bins.iter().find(|p| { match p.file_name().and_then(|s| s.to_str()) { @@ -69,39 +101,6 @@ fn try_add_files(files: &mut Vec, root: PathBuf) { } } -/// Returns a new `Layout` for a given root path. -/// The `root_path` represents the directory that contains the `Cargo.toml` file. - -pub fn project_layout(root_path: &Path) -> Layout { - let mut lib = None; - let mut bins = vec![]; - let mut examples = vec![]; - let mut tests = vec![]; - let mut benches = vec![]; - - let lib_canidate = root_path.join("src").join("lib.rs"); - if fs::metadata(&lib_canidate).is_ok() { - lib = Some(lib_canidate); - } - - try_add_file(&mut bins, root_path.join("src").join("main.rs")); - try_add_files(&mut bins, root_path.join("src").join("bin")); - - try_add_files(&mut examples, root_path.join("examples")); - - try_add_files(&mut tests, root_path.join("tests")); - try_add_files(&mut benches, root_path.join("benches")); - - Layout { - root: root_path.to_path_buf(), - lib: lib, - bins: bins, - examples: examples, - tests: tests, - benches: benches, - } -} - pub fn to_manifest(contents: &[u8], source_id: &SourceId, layout: Layout, -- 2.30.2